package bcxd.firstline.user.manager;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jt.classic.system.SimpleManager;
import jt.classic.system.group.GroupManager;
import jt.classic.system.role.IRole;
import jt.classic.system.role.RoleManager;

import org.limp.mine.EncryptTool;
import org.limp.mine.StringTool;

import bcxd.firstline.user.AppUser;

/**
 * 资源用户管理者
 * 
 * @author mido
 * 
 */
public class AppUserManager extends SimpleManager {
	public AppUserManager(Connection conn) {
		super(conn);
	}

	/**
	 * 资源用户登录校验
	 * 
	 * @param id
	 *            主键ID
	 * @return AppUser
	 */
	public AppUser getBcxdUserById(String id) {
		String sql = "SELECT * FROM BCXD_USER WHERE ID = ?";

		Map userinfo = this.trimmerI.searchSingleData(sql, id);
		if (userinfo == null) {
			return null;
		}

		AppUser user = new AppUser();

		userinfo.put("groups", getUserGroups(userinfo));
		userinfo.put("roles", getUserRoles(userinfo));
		String type = StringTool.checkString(userinfo.get("UTYPE"));

		if (type.equals("focalCompany") || type.equals("companySupplier")) {
			Map info = this.bwselectutil.selectSingleData("BCXD_FCAPPLY",
					(String) userinfo.get("UTYPEID"));
			userinfo.putAll(info);
		} else if (type.equals("creditOrganization")) {
			Map info = this.bwselectutil.selectSingleData("BCXD_CRAPPLY",
					(String) userinfo.get("UTYPEID"));
			userinfo.putAll(info);
		}

		user.reload(userinfo);

		return user;
	}

	/**
	 * 资源用户登录校验
	 * 
	 * @param logid
	 *            账号
	 * @param pwd
	 *            密码
	 * @return AppUser
	 */
	public AppUser checkAppUser(String logid, String pwd) {
		String sql = "SELECT * FROM BCXD_USER WHERE LOGID = ? AND PWD = ?";

		Map userinfo = this.trimmerI.searchSingleData(sql, logid, EncryptTool
				.encryptString(pwd));
		if (userinfo == null) {
			return null;
		}

		AppUser user = new AppUser();

		userinfo.put("groups", getUserGroups(userinfo));
		userinfo.put("roles", getUserRoles(userinfo));
		String type = StringTool.checkString(userinfo.get("UTYPE"));

		if (type.equals("focalCompany") || type.equals("companySupplier")) {
			Map info = this.bwselectutil.selectSingleData("BCXD_FCAPPLY",
					(String) userinfo.get("UTYPEID"));
			userinfo.putAll(info);
			
			/*if(type.equals("companySupplier")){
			 String userId = (String)userinfo.get("ID");
			   List list = this.bwselectutil.selectMultiData("BCXD_FCSUPPLIER", "SUP_ID", userId, null, null);
			}*/
		} else if (type.equals("creditOrganization")) {
			Map info = this.bwselectutil.selectSingleData("BCXD_CRAPPLY",
					(String) userinfo.get("UTYPEID"));
			userinfo.putAll(info);
		}

		user.reload(userinfo);

		return user;
	}

	@SuppressWarnings( { "rawtypes", "unchecked" })
	private List getUserGroups(Map userinfo) {
		List groups = new ArrayList();
		groups.add(GroupManager.getTempGroup());

		return groups;
	}

	/**
	 * 获取用户角色
	 * <p>
	 * bcxd注册用户拥有默认角色,角色key为用户type,需要事先在控制台定义
	 * </p>
	 * 
	 * @param userinfo
	 * @return
	 */
	private List getUserRoles(Map userinfo) {

		List roles = new ArrayList();
		RoleManager roleManager = new RoleManager(this.conn);
		IRole role = roleManager.getRoleByKey(((String) userinfo.get("UTYPE"))
				.toLowerCase());
		if (role != null) {
			roles.add(role);
		}

		return roles;
	}

	/**
	 * 资源用户账号密码校验
	 * 
	 * @param logid
	 *            账号
	 * @param pwd
	 *            密码
	 * @return boolean
	 */
	public boolean checkLogidPwd(String logid, String pwd) {
		String sql = "SELECT * FROM BCXD_USER WHERE LOGID = ? AND PWD = ?";

		return this.trimmerI.haveMoreData(sql, logid, EncryptTool
				.encryptString(pwd));
	}

	/**
	 * 密码重复性校验
	 * 
	 * @param logid
	 *            账号
	 * @return boolean
	 */
	public boolean reduplicateLogid(String logid) {
		String sql = "SELECT ID FROM BCXD_USER WHERE LOGID = ?";

		return this.trimmerI.haveMoreData(sql, logid);
	}
}
